package com.enniu.alipay.api.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.enniu.alipay.api.data.util.DataSourceSwitcher;
import com.enniu.alipay.api.domain.Article;
import com.enniu.alipay.api.domain.SearchUser;
import com.enniu.alipay.api.domain.User;
import com.enniu.alipay.api.repository.ArticleRepository;
import com.enniu.alipay.api.repository.UserRepository;
import com.enniu.alipay.api.service.UserService;

@Service
public class UserServiceImpl implements UserService {
	

	@PersistenceContext
	private EntityManager em;
	
	@Autowired
	private UserRepository userRepository;
	
	@Autowired
	private ArticleRepository articleRepository;

	@Override
	public User findUserById(Integer id) {
		if (id > 100) {
			DataSourceSwitcher.set("dataSource");
		} else {
			DataSourceSwitcher.set("dataSource1");
		}
		return userRepository.findOne(id);
	}

	@Override
	public List<User> findByUser(User user) {
		TypedQuery<User> query = em.createQuery("select a from User a where a.id = ?1", User.class);
		query.setParameter(1, user.getId());
		return query.getResultList();
	}
	
	
	@Override
	public Page<User> findAllUser(SearchUser searchUser) {
//		Sort sort = new Sort(Sort.Direction.DESC, "id");
//		Specification<Article> specification = getWhereClause(searchUser);
//		Page<Article> userList = articleRepository.findAll(specification,new PageRequest(searchUser.getPage() - 1, searchUser
//				.getLimit(), sort));
		return null;
	}

	private Specification<Article> getWhereClause(final SearchUser searchUser)
	{
		return new Specification<Article>() {
			@Override
			public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				List<Predicate> predicate = new ArrayList<>();
				if(searchUser.getUsername()!=null)
				{
					predicate.add(cb.like(root.get("username").as(String.class), searchUser.getUsername()));
				}
				if(!StringUtils.isEmpty(searchUser.getId()))
				{
					Join<Article,User> userJoin  = root.join(root.getModel().getSingularAttribute("user",User.class),JoinType.LEFT);
					predicate.add(cb.like(userJoin.get("username").as(String.class), "%"+searchUser.getUsername()+"%"));
				}
				Predicate[] pre = new Predicate[predicate.size()];
				return query.where(predicate.toArray(pre)).getRestriction();
			}
		};
	}

	@Override
	public List<User> findBySql(User user) {
		Query query= em.createNativeQuery("select * from T_User",User.class);
		List<User> list = query.getResultList();
		return list;
	}
	
}
